package cn.com.godliu.rabbitmqprovider.service.impl;

import cn.com.godliu.rabbitmqprovider.entity.UserInfo;
import cn.com.godliu.rabbitmqprovider.mapper.UserInfoMapper;
import cn.com.godliu.rabbitmqprovider.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {


    // 注入mapper类
    @Resource
    private UserInfoMapper userMapper;

    @Override
    public UserInfo addUser(UserInfo userInfo) {
        if (userInfo != null) {
            //result 是返回的影响行数
            int result = userMapper.insertSelective(userInfo);

            return userMapper.selectByPrimaryKey(userInfo.getId());
        }
        return null;
    }

    @Override
    public int removeUser(UserInfo userInfo) {
        if (userInfo != null) {
            return userMapper.deleteByPrimaryKey(userInfo.getId());
        }
        return 0;
    }

    @Override
    public UserInfo updateUser(UserInfo userInfo) {
        //result 是返回的影响行数
        int result = userMapper.updateByPrimaryKeySelective(userInfo);
        return userMapper.selectByPrimaryKey(userInfo.getId());
    }

    @Override
    public UserInfo getUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<UserInfo> getUsersByPage(int pageNum, int pageSize) {
        List<UserInfo> result = null;
        try {
            // 调用pagehelper分页，采用starPage方式。starPage应放在Mapper查询函数之前
            PageHelper.startPage(pageNum, pageSize); //每页的大小为pageSize，查询第page页的结果
            PageHelper.orderBy("id ASC "); //进行分页结果的排序
            result = userMapper.selectUser();
            //将查询到的数据封装到PageInfo对象
            PageInfo<UserInfo> pageInfo = new PageInfo(result, pageSize);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public PageInfo<UserInfo> getUsersofPageInfoByPage(int pageNum, int pageSize) {
        PageInfo<UserInfo> pageInfo = null;
        try {
            // 调用pagehelper分页，采用starPage方式。starPage应放在Mapper查询函数之前
            PageHelper.startPage(pageNum, pageSize); //每页的大小为pageSize，查询第page页的结果
            PageHelper.orderBy("id ASC "); //进行分页结果的排序
            List<UserInfo> result = userMapper.selectUser();
            //将查询到的数据封装到PageInfo对象
            pageInfo = new PageInfo(result, pageSize);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return pageInfo;
    }

    @Override
    public List<UserInfo> getUsersByPage(int age, int pageNum, int pageSize) {
        List<UserInfo> result = null;
        try {
            // 调用pagehelper分页，采用starPage方式。starPage应放在Mapper查询函数之前
            PageHelper.startPage(pageNum, pageSize); //每页的大小为pageSize，查询第page页的结果
            PageHelper.orderBy("id ASC "); //进行分页结果的排序
            result = userMapper.selectUserByAge(age);
            //将查询到的数据封装到PageInfo对象
            PageInfo<UserInfo> pageInfo = new PageInfo(result, pageSize);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
